<template>
  <div class="login-page">
    <img class="left-img" src="@/assets/img/login_bg.png" />
    <img class="right-bot-img" src="@/assets/img/login_r_b.png" alt="" />
    <img class="logo" src="@/assets/img/login_logo.png" alt="" />
    <el-form ref="form" :model="loginForm" :rules="loginRules" class="login-form-wrap" @keydown.enter="handleLogin">
      <h1 class="form-title">弘则研究后台管理系统</h1>
      <el-form-item prop="account">
        <el-input v-model="loginForm.account" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" />
      </el-form-item>
      <el-checkbox v-model="keepLogin" style="margin: 30px 0 10px 0">保持登录态</el-checkbox>
      <el-button type="primary" style="width: 100%" @click="handleLogin">登录</el-button>
    </el-form>
  </div>
</template>
<script lang="ts">
import { Base64 } from "js-base64";
import md5 from "js-md5";
import { defineComponent, reactive, toRefs, ref } from "vue";
import { useRouter } from 'vue-router'
import { apiLogin } from "@/api/common";
import {useStore} from "vuex";

export default defineComponent({
  name: "Login",
  components: {},
  setup() {
    
    const form = ref(null);
    const router=useRouter()
    const store=useStore()
    
    
    //获取本地保存的账号密码信息
    const accout=localStorage.getItem('account')
    const password=localStorage.getItem('password')
    //表单数据
    const formData = reactive({
      loginForm: {
        account: accout||"",
        password: password?Base64.decode(password):"",
      },
      loginRules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      keepLogin: true, //是否保存登录态
    });

    //登录
    const handleLogin = () => {
      (form.value as any).validate(async (valid: boolean) => {
        if (valid) {
          let res= await apiLogin({
            Username: formData.loginForm.account,
            Password: md5(formData.loginForm.password),
          });
          if(res.Ret===200){
            if(formData.keepLogin){
              let base64PWD=Base64.encode(formData.loginForm.password)
              localStorage.setItem('password',base64PWD)
              localStorage.setItem('account',formData.loginForm.account)
            }else{
              localStorage.removeItem('account')
              localStorage.removeItem('password')
            }
            store.commit('user/setUser', res.Data)
            router.replace('/workbench/dashboard')
          }
        } 
      });
    };

    return {
      form,
      ...toRefs(formData),
      handleLogin,
    };
  },
});

// import { Options, Vue } from "vue-class-component";
// import { ElForm, ElFormItem, ElInput } from "element-plus";
// import { reactive, toRefs, onMounted } from "@vue/runtime-core";

// @Options({
//   name: "Login",
//   components: {
//     [ElForm.name]: ElForm,
//     [ElFormItem.name]: ElFormItem,
//     [ElInput.name]: ElInput,
//   }
// })

// export default class Login extends Vue {
//   setup() {

//     onMounted(() => {
//       console.log("111");
//     });

//     const formData = reactive({
//       loginForm: {
//         account: "a",
//         password: "",
//       },
//       loginRules: [{}],
//     });

//     return {
//       ...toRefs(formData)
//     };
//   }
// }
</script>

<style lang="scss" scoped>
.login-page {
  width: 100%;
  height: 100%;
  position: relative;
}
.login-form-wrap {
  position: absolute;
  width: 500px;
  bottom: 35%;
  right: 15%;
  .form-title {
    text-align: center;
    font-size: 32px;
    margin-bottom: 50px;
  }
}
.left-img {
  width: 46%;
  position: absolute;
  left: 0;
  bottom: 0;
  display: block;
}
.right-bot-img {
  width: 8%;
  position: absolute;
  right: 0;
  bottom: 0;
}
.logo {
  width: 16%;
  position: absolute;
  top: 44px;
  right: 160px;
}
</style>
